package com.bradz.mymovies;

import java.util.List;

import android.app.ActivityManager;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.ToggleButton;

public class ApplicationGlobalVars extends Application{
	private boolean client_set = false;
	private boolean played_something = false;
	private boolean mute = false;
	private boolean lock = false;
	private String global_buffer = "";
	private String developer_payload = "";
	//This is the network object. Expensive to create so it's here
	private ClassClientHolder httpclient;
	//These are the static elements that have to be kept global 
	//so the socket can reference them when they are called
	private ClassDeveloperPayload payload = new ClassDeveloperPayload();
	
	public void set_buffer(String global_buffer){
		this.global_buffer = global_buffer;
	}
	
	public String get_buffer(){
		return global_buffer;
	}
	
	public void set_mute(boolean mute){
		this.mute = mute;
	}
	
	public boolean get_mute(){
		return mute;
	}
	
	public void set_lock(boolean lock){
		this.lock = lock;
	}
	
	public boolean get_lock(){
		return lock;
	}
	
	public void reset_media_intent(String developer_payload){

		if(this.developer_payload != developer_payload){
			
			this.developer_payload = developer_payload;
			
			Log.i("ApplicationGlobalVars","Sending Intent!");
			
			Intent intent = new Intent(getBaseContext(), ActivityMediaPlayer.class);
	        intent.putExtra("PATH_ID", global_buffer);
	        
			ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
			List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1); 
	        
	        if(taskInfo.get(0).topActivity.getClassName().equals("com.bradz.mymovies.ActivityMediaPlayer")){
	            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        }else{
	            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	        }
	        
	        startActivity(intent);
		}
		
	}
	
    public boolean client_loaded(){
    	return client_set;
    }
    
    public void did_play(){
    	played_something = true;
    }
    
    public boolean played_something(){
    	return played_something;
    }
    
    public void invalidate_client_store(){
    	client_set = false;
    }
    
    public void set_client_store(ClassClientHolder httpclient){
    	client_set = true;
    	this.httpclient = httpclient;
    }
    
    public ClassClientHolder get_client_store(){
    	return httpclient;
    }
    
    public String get_developer_payload(){
    	return payload.get_payload();
    }
}
